/*
 * Copyright (c) 2012 Sean Connelly
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

var config = require('./config.js');
var common = require('./common.js');

//
// warning system for compiler
//

var msgFmts = {
		//           code:level: message
		ParenOnNewLine: 'RW010001:1: open parenthesis starts on new line (use `eval (expression)` ' +
			'to use an expression as a statement)',
		BraceOnNewLine: 'RW010002:1: open brace starts on new line (use `eval {table}` to use a ' +
			'table at the start of a statement)'
	};

// must redirect through a function to get the closures
var genFunc = function(name, lev, msg, wc) {
		return function() {
				var m = msg;
				for (var i = arguments.length - 1; i >= 0; i--) // reverse order so $10 is replaced before $1
					m = common.replaceAll(m, '$' + i, arguments[i]);
				return {
						code: wc,
						level: lev,
						message: m
					};
			};
	};

var re = new RegExp('^([^:]+):([1-9][0-9]*): (.*)$');
for (var warn in msgFmts)
{
	var sp = msgFmts[warn].match(re);
	if (sp === null)
		throw 'invalid warning: ' + msgFmts[warn];
	else
		exports[warn] = genFunc(warn, parseInt(sp[2], 10), sp[3], sp[1]);
}
